//mui.alert('拖动圆的瞄点,调整圆形电子围栏的位置和大小.');
//mui("#demo1").progressbar({progress:20}).show();
var _data = {

	    "currentUser": {
	        "userId": 132,
	        "userName": "cs\u91CE\u6218",
	        "lon": 116,
	        "lat": 40,
	        "userType": 1,
	        "color": "#CC33FF"
	    },
	    "gpsData": [
	        {
	            "userId": 132,
	            "userName": "cs\u91CE\u6218",
	            "lon": 116,
	            "lat": 40,
	            "userType": 0,
	            "color": "#CC33FF"
	        }
	    ],
	    "grounp": {
	        "area": "\u9655\u897F\u5730\u533A",
	        "userId": 130,
	        "runState": 0,
	        "playerTime": 20,
	        "fenceLon": 116,
	        "fenceLat": 40,
	        "fenceRadius": 940,
	        "isDefence": false,
	        "checkCode": "123456",
	        "roomCount": 10,
	        "remainTime": 564,
	        "fireTime": 10,
	        "id": 171,
	        "name": "\u7BA1\u7406\u5458\u6D4B\u8BD5"
	    },
	    "room": {
	        "grounpId": 171,
	        "checkCode": "123456",
	        "userCount": 0,
	        "isCurrentUser": false,
	        "userId": 132,
	        "runState": 0,
	        "id": 628,
	        "name": "\u6697"
	    },
	    "life": {
	        "id": 99,
	        "userId": 132,
	        "bulletCount": 100,
	        "lifeValue": 65,
	        "fightScore": 0,
	        "setLifeValue": 100,
	        "setBulletCount": 100,
	        "goldCoin": 9,
	        "userName": null
	    },
	    "ip": "119.29.10.36"
	
}

var markArray = new Array();
var _knapsackDetailList;
var mapMode = 1;
var isSet = false;
var satelliteLayer;
var roadNetLayer;
var isPlayerShow = false;
var isFirst = false;
//console.log(1);
//ShowLogInfo("hello");
//CreateInitMap();
//CreateHitEffectionMarker();

//setInterval("CheckGetKnapsack()", 1000); //1000为1秒钟

// if (isTest) {
// 	CreateMapAndMarker(_data);
// }

//ShowLogInfo("123");
//CreateMapAndMarker(_data);
var map;
function CreateInitMap() {
	map = new AMap.Map('container', {
		resizeEnable: true,
		center: [0, 0],
		showLabel: true,
		mapStyle: "amap://styles/light",
		zoom: 13,
		showIndoorMap: false,
		//features:['road','building']
	});
	// 构造官方卫星、路网图层
	//var satelliteLayer = new AMap.TileLayer.Satellite();
	//var roadNetLayer =  new AMap.TileLayer.RoadNet();

	//批量添加图层
	//map.add([satelliteLayer, roadNetLayer]);
	//ShowLogInfo("createmap");
}
//玩家端地图调用，刷新地图
function CreateMapAndMarker(data) {
	try {
		if(map==null)
		{
			CreateInitMap();
		}
	  _StartCreate(data);
	} catch (error) {
	  //window.location.href = "uniwebview://Debug?json="+error.message;
	}

}


var _server_ip;
function _StartCreate(data) {

	//DeleteAllMarker();
	_server_ip = data.ip;
	SetLifeMesage(data);
	SetUserValue(data);
	SetOnLineCount(data);

	if (!isFirst) {
		map.setCenter([data.currentUser.lon, data.currentUser.lat]);
		// CreateMap(data.currentUser.lon,data.currentUser.lat);
		SetHander();
		setTimeout(function() {
			SetFitView();
		}, "1000");

		isFirst = true;

	} else {
		map.center = [data.currentUser.lon, data.currentUser.lat];
	}

	DeletePlayerMark(data.currentUser.userId);
	CreatePlayerMarker(data.gpsData, data.currentUser.userId);
	CreateCircle(data.grounp);
	//检查游戏状态
	TimerGetGameRunState(data.currentUser.userId, data.currentUser.userType, data.ip);
	//设置左侧菜单
	//管理员
	if (data.currentUser.userType == 1) {
		document.getElementById('playerShowC').style.display = "none";
	}
	//玩家
	else{
		document.getElementById('admin_playerShowC').style.display = "none";
	}
}

function CreateKnapsackInfo(data) {

	_knapsackDetailList = data.knapsackInfo;
	ShowMarkerInfo(_knapsackDetailList);
	//创建背包物资
}

//地图样式
function SwichMap() {

	var mapButton = document.getElementById("switchMap");
	//移除卫星地图
	if (satelliteLayer) {
		map.remove(satelliteLayer);
		satelliteLayer=null;
	}
	
	if (roadNetLayer) {
		map.remove(roadNetLayer);
		roadNetLayer =null;
	}
	
	mapMode++;
	//标准地图
	if (mapMode % 2 == 1) {
		mapButton.innerHTML = "卫星地图";
		var styleName = "amap://styles/light";
		map.setMapStyle(styleName);
		SetCircleStyle(false);
	} else if (mapMode % 2 == 0) {
		mapButton.innerHTML = "标准地图";
		satelliteLayer = new AMap.TileLayer.Satellite();
		roadNetLayer = new AMap.TileLayer.RoadNet();
		map.setMapStyle();
		map.add(satelliteLayer);
		map.add(roadNetLayer);
		SetCircleStyle(true);
	}
	isSet = true;
}

var circle;

function CreateCircle(grounp) {
	if (grounp.fenceLon > 0) {
		if (circle == null) {
			circle = new AMap.Circle({
				center: [grounp.fenceLon, grounp.fenceLat],
				radius: grounp.fenceRadius, //半径
				borderWeight: 3,
				strokeColor: "#1E9FFF",
				strokeWeight: 6,
				strokeOpacity: 0.4,
				fillOpacity: 0.3,
				strokeStyle: 'dashed',
				strokeDasharray: [10, 10],
				// 线样式还支持 'dashed'
				fillColor: '#FF5722',
				zIndex: 50,
			})

			circle.setMap(map)
		} else {
			if (grounp.fenceRadius <= 0) {
				circle.setRadius(0.1);
			} else {
				circle.setRadius(grounp.fenceRadius);
			}

		}

	}

}

function SetCircleStyle(isNormal) {
	if (!isNormal) {
		circle.setOptions({


			borderWeight: 3,
			strokeColor: "#1E9FFF",
			strokeOpacity: 1,
			strokeWeight: 6,
			strokeOpacity: 0.2,
			fillOpacity: 0.3,
			strokeStyle: 'dashed',
			strokeDasharray: [10, 10],
			// 线样式还支持 'dashed'
			fillColor: '#FF5722',
		});
	} else {
		circle.setOptions({

			borderWeight: 5,
			strokeColor: "#FF5722",
			strokeOpacity: 0.8,
			strokeWeight: 5,
			fillOpacity: 0.1,
			strokeStyle: 'dashed',
			fillColor: '#1E9FFF',

		});
	}
	//console.log(circle.fillOpacity);
}

var currentPosition;
var currrentMark;



// 编辑端创建marker
function Editor_CreateCurrentMarker()
{
	//ShowLogInfo("startcreatemarker");
	var url = "image/locate/my.png";
	var icon = new AMap.Icon({
		size: new AMap.Size(32, 32), // 图标尺寸
		image: url, // Icon的图像
		imageSize: new AMap.Size(32, 32) // 根据所设置的大小拉伸或压缩图片
	});
	
	currrentMark = new AMap.Marker({
		map: map,
		icon: icon,
		anchor: "top-center",
		anchor: 'center',
		offset: new AMap.Pixel(0, 0), // 设置点标记偏移量
		position: [116.29646, 40.100166],
	});
	//ShowLogInfo("self marker finish");
	
}



function SetHander() {

	// 添加事件监听, 使地图自适应显示到合适的范围
	AMap.event.addDomListener(document.getElementById('setFitView'), 'click', function() {
		SetFitView();
		// document.getElementById('centerCoord').innerHTML = '当前中心点坐标：' + newCenter.getCenter();
		//document.getElementById('tips').innerHTML = '通过setFitView，地图自适应显示到合适的范围内,点标记已全部显示在视野中！';
	});
}

function SetFitView() {
	var overlaysList = map.getAllOverlays('Circle');

	//2.使用setFitView方法
	map.setFitView(overlaysList);

	//map.setFitView();
}

function ShowMapEffection(str) {
	// var jsonstr = JSON.stringify(str);
	// var data = JSON.parse(jsonstr);
	// var playerId = data.userId;
	// var isHit = data.isHit;
	// for (var i = 0; i < markArray.length; i++) {
	// 	if (markArray[i].getExtData() == playerId && isHit) {
			
	// 		ShowEffectionMark(markArray[i].getPosition());
	// 		break;
	// 	}

	// }
	ShowEffectionMark(circle.getCenter());
}
//创建击中后的效果
var effectionMarkerObject;
function CreateHitEffectionMarker()
{
	effectionMarkerObject = new AMap.Marker({
		map: map,
		position: [108.963393,34.274106],
		offset: new AMap.Pixel(25, -23)
	});
	var markerContent =
		`<div>
									<img src="image/fire.gif" style="width:102px;height:73px"> 
								   </div>`;
	effectionMarkerObject.setContent(markerContent);
	effectionMarkerObject.hide();
}

var setTimeOutFun;
function ShowEffectionMark(lngla) {
	
	effectionMarkerObject.show();
	effectionMarkerObject.setPosition(lngla);
	if(setTimeOutFun)
	{
		clearTimeout(setTimeOutFun);
		setTimeOutFun  = null;
	}
	setTimeOutFun = setTimeout(function() {
		effectionMarkerObject.hide();
	}, 5000)
}

//var currentPosition = [0,0];
function UpdataPosition(jsonObject) {
	var lon = jsonObject.lon;
	var lat = jsonObject.lat;

	//currentPosition = [lon, lat];
	if (!CheckNull(currrentMark)) {

		currrentMark.setPosition([lon, lat]);
	}

}

function UpdateArrowRotation(angle) {
	if (!CheckNull(currrentMark)) {
		currrentMark.setAngle(angle);
	}
}

function SetMarquee(content) {
	//document.getElementById("font1").innerHTML = content.data;
}






//打印日志
function ShowLogInfo(json)
{
	// if(_server_ip=="" || _server_ip==null)
	// {
	// 	_server_ip = "119.29.10.36";
	// }
	// let myurl ="http://" + _server_ip + ":8899/SaveTestData";
	// alert(myurl);
	// const sdata = { data: json };
	// axios.post(myurl, sdata).then(function (response) {
	// 	alert(response);
	//   })
	//   .catch(function (error) {
	// 		alert(error);
	//   });
	
	//var myurl = "uniwebview://showLogInfo?json="+json;
	//window.location.href = myurl;
	
	
}
